

//
//  XLQYExchangeView.m
//  XiangLove
//
//  Created by 李传坤 on 2018/7/12.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLQYExchangeView.h"
@interface XLQYExchangeView ()<UITextFieldDelegate>

@property (strong, nonatomic) UIImageView *LeImageView;
@property (strong, nonatomic) UILabel *xiuBeansLabel;

@property (strong, nonatomic) UIView *alertView;

@property (strong, nonatomic) UIButton *sureExchange;
@property (strong, nonatomic) UIButton *dismissButton;
@property (strong, nonatomic) UIButton *rechargeButton;
@property (strong, nonatomic) NSString *beansNumber;
@property (assign, nonatomic) NSInteger kaiguanNumber;
@end

@implementation XLQYExchangeView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置背影为透明色
        
        self.backgroundColor = [UIColor clearColor];
        [self addViews];
    }
    return self;
}
- (void)addViews{
    _kaiguanNumber = 59;
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.4;
    [self addSubview:view];
    
    _alertView = [[UIView alloc]init];
    _alertView.backgroundColor = [UIColor whiteColor];
    _alertView.layer.cornerRadius = 10;
    _alertView.center = CGPointMake(self.width/2,self.height/2);
    _alertView.frame = CGRectMake(SCREEN_WIDTH/2 - 145, SCREEN_HEIGHT/2 - 160, 290, 280);
    [self addSubview:_alertView];
    
    _LeImageView = [[UIImageView alloc]init];
    _LeImageView.frame = CGRectMake(98, 95, 18, 18);
    _LeImageView.image = [UIImage imageNamed:@"common_qian_icon_xiu_n"];
    [_alertView addSubview:_LeImageView];
    _xiuBeansLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 45, 290, 40)];
    _xiuBeansLabel.textColor = [UIColor blackColor];
    _xiuBeansLabel.textAlignment = NSTextAlignmentCenter;
    _xiuBeansLabel.font = [UIFont systemFontOfSize:38];
    [_alertView addSubview:_xiuBeansLabel];
    
    _XiubiLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 94, 250, 20)];
    _XiubiLabel.text = @"我的秀币";_XiubiLabel.textAlignment = NSTextAlignmentCenter;
    _XiubiLabel.textColor = [UIColor darkGrayColor];
    _XiubiLabel.font = [UIFont systemFontOfSize:14];
    [_alertView addSubview:_XiubiLabel];
    
//    _textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 88, 250, 40)];
//    _textField.placeholder = @" 至少兑换60金豆";
//    _textField.textColor = [UIColor blackColor];
//    _textField.delegate = self;
//    _textField.keyboardType = UIKeyboardTypeNumberPad;
//    _textField.delegate = self;_textField.tintColor = RGB_HEX(0xffbb01);
//    _textField.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    _textField.font = [UIFont systemFontOfSize:18];
//    [_alertView addSubview:_textField];
    
//    _middleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 142, 250, 20)];
//    _middleLabel.text = @"可兑换0秀币";
//    _middleLabel.textColor = RGB_HEX(0xffbb01);
//    _middleLabel.font = [UIFont systemFontOfSize:14];
//    [_alertView addSubview:_middleLabel];
    
    _dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_dismissButton setBackgroundImage:[UIImage imageNamed:@"Views_but_chacha_n"] forState:UIControlStateNormal];
    [_dismissButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:_dismissButton];
    [_dismissButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_alertView.mas_top).offset(5);
        make.right.equalTo (_alertView.mas_right).offset(-15);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    _sureExchange = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sureExchange addTarget:self action:@selector(recharge) forControlEvents:UIControlEventTouchUpInside];
    [_sureExchange setTitle:@"立即充值" forState:UIControlStateNormal];
    _sureExchange.backgroundColor = RGB_HEX(0xFFD106);
    _sureExchange.layer.cornerRadius = 5;
    [self.alertView addSubview:_sureExchange];
    [_sureExchange mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_alertView.mas_centerX);
        make.bottom.equalTo (_alertView.mas_bottom).offset(-104);
        make.size.mas_equalTo(CGSizeMake(250, 40));
    }];
    _rechargeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rechargeButton.layer.cornerRadius = 5;
    _rechargeButton.layer.borderWidth = 1;
    _rechargeButton.layer.borderColor = RGB_HEX(0xffbb01).CGColor;
    [_rechargeButton addTarget:self action:@selector(exchange) forControlEvents:UIControlEventTouchUpInside];
    [_rechargeButton setTitle:@"50金豆换5000秀币" forState:UIControlStateNormal];
    [_rechargeButton setTitleColor:RGB_HEX(0xffbb01) forState:UIControlStateNormal];
    [self.alertView addSubview:_rechargeButton];
    [_rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_alertView.mas_centerX);
        make.bottom.equalTo (_alertView.mas_bottom).offset(-54);
        make.size.mas_equalTo(CGSizeMake(250, 40));
    }];
    
    _BottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 244, 250, 20)];
    _BottomLabel.text = @"秀币仅限于直播送礼, 玩游戏";
    _BottomLabel.textColor = kDarkGrayColor;
    _BottomLabel.textAlignment = NSTextAlignmentCenter;
    _BottomLabel.font = [UIFont systemFontOfSize:14];
    [_alertView addSubview:_BottomLabel];
}
- (void)exchange{
    if  ([_beansNumber integerValue] < 50){
        [self recharge];
        [MBProgressHUD showContent:@"金豆不足 请充值"];
    }else{
        [self dismiss];
        _block(2,@"50");
    }

}
- (void)recharge{
    [self dismiss];
    _block(1,@"");
}
-(void)show{
    [[[XLHTTPSessionManager sharedManager] XLV2GET:@"/api/v2/Balance/GetUserBalance" parameters:@{@"userId":[[XLAccountManager shareInstance]model].Id} resultClass:nil]subscribeNext:^(id x) {
        [[[[UIApplication sharedApplication]delegate]window]addSubview:self];
        _beansNumber = [NSString stringWithFormat:@"%.0f",[[x objectForKey:@"AccountBalance"] floatValue] * 10];
        __block NSString *xiubi;

        NSLog(@"%@",xiubi);
        NSLog(@"888888888%@666666666",xiubi);
        _middleLabel.text = [NSString stringWithFormat:@"可兑换%ld秀币",(long)[_beansNumber integerValue]*100];
    } error:^(NSError *error) {
        
    }];
}
-(void)dismiss{
    [UIView animateWithDuration:0.3
                          delay:0.0
                        options: UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.alpha = 0;
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                     }];
}



@end
